package cn.yangliu.monitor;

import cn.yangliu.config.XlsxChangeEvent;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * @author 问道于盲
 * @date 2021-10-03
 */
@Component
public class XlsxListenerAdaptor extends FileAlterationListenerAdaptor implements ApplicationContextAware {

    private ApplicationContext context;

    /**
     * 被监视文件改变触发
     *
     * @param file 被监视的文件
     */
    @Override
    public void onFileChange(File file) {
        context.publishEvent(new XlsxChangeEvent(file));
    }

    /**
     * 设置spring上下文
     *
     * @param applicationContext applicationContext
     * @throws BeansException 异常
     */
    @Override
    public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
